package cn.qdhlm.collectservice.common.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Name: ComUtil
 * @Description: ComUtil
 * @Author: jiangcs
 **/
public class ComUtil {
    public static boolean isEmpty(Object object) {
        return StringUtils.isEmpty(object);
    }

    /**
     * is contain
     *
     * @param source
     * @param key
     * @return
     */
    public static boolean isContain(Object source, Object key) {
        if (!ComUtil.isEmpty(source) && !ComUtil.isEmpty(key)) {
            if (source.getClass().isArray()) {
                Object[] sourceArr = (Object[]) source;
                for (int i = 0; i < sourceArr.length; i++) {
                    if (key.equals(sourceArr[i])) {
                        return true;
                    }
                }
            } else {
                return ("," + source + ",").contains("," + key + ",");
            }
        }
        return false;
    }

    /**
     * safeString
     *
     * @param object
     * @return
     */
    public static String safeString(Object object) {
        if (isEmpty(object)) {
            return "";
        }
        return String.valueOf(object);
    }

    /**
     * safeInt
     *
     * @param object
     * @return
     */
    public static int safeInt(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            if (object instanceof Double) {
                return ((Double) object).intValue();
            }
            if (object instanceof Float) {
                return ((Float) object).intValue();
            }
            return Integer.parseInt(safeString(object));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * Object 2 Double
     *
     * @param object
     * @return
     */
    public static double safeDouble(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            return Double.parseDouble(safeString(object));
        } catch (Exception e) {
            return 0D;
        }
    }

    /**
     * 格式化日期时间
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String formatDateTime(Date date, String pattern) {
        if (isEmpty(date)) {
            return "";
        }
        if (isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 根据length 左补零
     *
     * @param object
     * @param length
     * @return
     */
    public static String fillZeroLeft(Object object, int length) {
        StringBuilder sbStr = new StringBuilder(safeString(object));
        int lengthDiff = length - sbStr.length();
        if (lengthDiff > 0) {
            for (int i = 0; i < lengthDiff; i++) {
                sbStr.insert(0, "0");
            }
        }
        return sbStr.toString();
    }

    /**
     * 格式化 Double类型 小数
     *
     * @param d
     * @param scale
     * @return
     */
    public static Double formatScale(Double d, int scale) {
        BigDecimal bigDecimal = BigDecimal.valueOf(d).setScale(scale, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }
}
